<?php
require_once '/../Controleur/Forum_controleur.php';
require_once '/../Controleur/User_controleur.php';
    //AFFICHAGE des messages du topic

if (isset($_GET['idTopic'])|| isset($_SESSION['idTopic'])) {
    if(isset($_GET['idTopic'])){
        $idTopic = $_GET['idTopic'];
        $_SESSION['idTopic'] =$idTopic;
    }else if(isset($_SESSION['idTopic'])){
        $idTopic = $_SESSION['idTopic'];
    }
        
//        echo ( $_GET['idTopic']);
        $listeMessages = Forum_controleur::afficherListeMessages($idTopic);?>
        <div class="Forum_vue">
    <h3>Forum Mapping Dead</h3> 
    <h3>Messages</h3>
        <div class="forumTopic">
          <table id='messages' >
<?php        foreach ($listeMessages as $message) {
            $user_controleur = new User_controleur;
            $idUser= $message->getIdUser();
            $user = $user_controleur->getInfo($message->getIdUser());
            $loginUser = $user->getLogin();
            $idMessage = $message->getIdForumMessage();
            $libelleMessage = $message->getMessage();
            $dateCreationMessage = $message->getDateCreation();
            ?>
            <form  method='GET'>
                <input type="hidden" name="idMessage" value="<?php echo $idMessage ?>"/>
            </form>
            <tr><th colspan="2"><?php echo $dateCreationMessage; ?></th></tr>
            <tr><td style="width: 10%;"><?php echo $loginUser ?></td>
                <td id='normal'><?php echo $libelleMessage ?></td>
                <!--<td><textarea required rows="4" cols="50" id="newMessage" value=""></textarea><td>-->
                    <?php
                if (isset($_SESSION['username']) && ( ($_SESSION['statut'] == 2) || ($_SESSION['idUser'] == $idUser))) {
//                    echo "<input type ='button' onclick='afficheModif' id='modifMess' value='Modifier Message'>";
                    echo "<td><button type ='button' onclick='suppMessage($idMessage)'>Supprimer Message</button></td></tr>";
                }
                ?>
            </tr>
            </a><br/>
            <?php
        }
        ?>
        </table></div>
        <?php
        if (isset($_SESSION['username'])) {
         ?>
            </table> <br><h3>Ajout d'un message</h3>
        <form id="addMessage" >
            <table>
                <tr><input type="hidden" id="idTopic" value="<?php echo $idTopic ?>"/>         
                    <td><label >Message : </label></td><td><textarea required rows="4" cols="50" id="newMessage" value=""></textarea></td></tr>
            <tr><td></td><td><button class="btnMessage" type="submit" >Ajouter un message</button></td></tr>
             </table> 
        </form>
        <?php
    } else {
        echo "<br><h3>Veuillez vous connecter pour poster des messages sur le forum</h3>";
    }
}else{
    echo "<h3>Aucun message à afficher<h3>";
}
?>
<script>
        function page(nomPage) {

		var chemin = 'src/Vue/'+nomPage+'.php';
		$.ajaxSetup({async: false});
		$.post("src/Vue/session.php",{"chemin" : chemin});		
		$.ajaxSetup({async: true});
                $("#contenu").load('src/Vue/'+nomPage+'.php');
                if(nomPage == "User_vue" || nomPage == "Carte_vue") {
			location.reload();
		}
            }
            
         function suppMessage(idMessage){
             $.ajaxSetup({async: false});
            $.post("src/Vue/verif.php",
                    {
                        idMessageSup: idMessage
                    });
            $.ajaxSetup({async: true});
             location.reload();
         }


        $('#addMessage').submit(function () {
            //            alert($("#newMessage").val());
            $.ajaxSetup({async: false});
            $.post("src/Vue/verif.php",
                    {
                        newMessage: $("#newMessage").val(),
                        idTopic: $("#idTopic").val()
                    });
            $.ajaxSetup({async: true});
            //        location.reload();

        });
    </script>